/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MidiGui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.midi.MidiUnavailableException;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import midi.MidiPlayer;
import music.Chords.Chord;
import music.Note;
import music.types.NoteTypes;
import structures.NotesManager;

/**
 *
 * @author TJ
 */
public class ChordPanel extends JPanel{
    private MidiPlayer mp;
    private StaffPanel s;
    private JButton b;
    private JTextField t;
    public ChordPanel() {try{init();}catch(Exception e){}}
    
    private void init() throws MidiUnavailableException {
        s=new StaffPanel();
        mp=MidiPlayer.getMidiPlayer();
        t=new JTextField();
        this.setLayout(new BorderLayout());
        this.add(s,BorderLayout.CENTER);
        b=new JButton("Preview");
        b.addActionListener(new ChordPreview());
        JPanel south=new JPanel();
        south.setLayout(new GridLayout(1,2));
        south.add(t);
        south.add(b);
        this.add(south,BorderLayout.SOUTH);
    }
    public void populateNotes(Chord n2) {
        NotesManager n=s.getStaff(0).getMeasure(0).getNotes();
        n.clearAllNotes();
        n.getVoice(1).addNoteAt(0, n2);
        repaint();
    }
    class ChordPreview implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            Chord c=Chord.parse(NoteTypes.QUARTER, t.getText());
            populateNotes(c);
            mp.playNote(c, 2000);
            
        }
    
    }
}
